/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canvas;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import midlet.MainMidlet;
import util.SpriteClass;

/**
 *
 * @author aisyah
 */
public class LanguageCanvas extends Canvas implements CommandListener {

    private MainMidlet midlet;
    private Displayable displayable;
    int index = 0;

    public LanguageCanvas(MainMidlet midlet, Displayable returnScreen) {
        this.midlet = midlet;
        this.displayable = returnScreen;

    }

    public LanguageCanvas(MainMidlet midlet) {
        this.midlet = midlet;
    }

    protected void paint(Graphics g) {
        try {
            setFullScreenMode(true);
            // set background image
            SpriteClass bg = new SpriteClass(Image.createImage("/res/bg_lg.png"));
            bg.setPosition(0, 0);

            SpriteClass button[] = new SpriteClass[2];
            for (int i = 0; i < 2; i++) {
                System.out.println(index + " all= " + i);
                if (index % 2 == i) {
                    button[i] = new SpriteClass(Image.createImage("/res/btn/hvr/h_btn-lg-" + i + ".png"));
                    System.out.println("  " + index + " mod= " + i);
                } else {
                    button[i] = new SpriteClass(Image.createImage("/res/btn/btn-lg-" + i + ".png"));
                    System.out.println("  " + index + " No mod= " + i);
                }
                button[i].setPosition(15 + (120 * i), 180);
            }

            LayerManager lmng = new LayerManager();

            for (int i = 0; i < 2; i++) {
                lmng.append(button[i]);
            }
            lmng.append(bg);
            lmng.paint(g, 0, 0);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Called when a key is pressed.
     */
    protected void keyPressed(int keyCode) {

        if (keyCode == -3) {
            // kiri
            index -= 1;
        } else if (keyCode == -4) {
            // kanan
            index += 1;
        } else if (keyCode == -5) {
            // fire button
            goMenu();
        } else if (keyCode == -6) {
            Display.getDisplay(midlet).setCurrent(displayable);
        } else if ( keyCode == -7){
            this.midlet.destroyApp(true);
        }
        repaint();

    }

    private void goMenu() {
        int posisi = index % 2;
        System.out.println("posisi==========>" + posisi);
        if (posisi==0) {
                MenuCanvas mnCvs = new MenuCanvas(midlet, Display.getDisplay(midlet).getCurrent(),"eng");
                Display.getDisplay(midlet).setCurrent(mnCvs);
                System.out.println("===========execute go Menu Eng =============");
        }else{
                MenuCanvas mnCvs = new MenuCanvas(midlet, Display.getDisplay(midlet).getCurrent(), "arb");
                Display.getDisplay(midlet).setCurrent(mnCvs);
                System.out.println("===========execute go Menu Arb =============");
        }
    }
    /**
     * Called when action should be handled
     */
    public void commandAction(Command c, Displayable d) {
    }

    
}
